﻿using System;
using System.Collections.Generic;
using System.Web;
using Rhino.Mocks;
using NUnit.Framework;

namespace mForum.Web.Common.Tests.Infrastructure.SessionTests
{
    using mForum.Web.Common.Infrastructure;

    [TestFixture]
    public class SessionTests
    {
        private IHttpContextProvider _httpContextProvider;
        private ISessionManager _sessionManager;
        private HttpSessionStateBase _httpSessionStateBase;

        [SetUp]
        public void Setup()
        {
            _httpContextProvider = MockRepository.GenerateStub<IHttpContextProvider>();
            _httpSessionStateBase = MockRepository.GenerateMock<HttpSessionStateBase>();
            _httpContextProvider.Stub(c => c.Context.Session).Return(_httpSessionStateBase);

            _sessionManager = new SessionManager(_httpContextProvider);
        }

        [Test]
        public void can_set_session_varibale()
        {
            string key = "test_key";
            string excpetedValue = "test";
            _sessionManager.Set<string>(key, excpetedValue);

            _httpSessionStateBase.AssertWasCalled(s => s.Add(key, excpetedValue));
        }

        [Test]
        public void can_get_session_variable()
        {
            string key = "test_key";
            string excpectedValue = "test";

            _httpSessionStateBase.Stub(c => c[key]).Return(excpectedValue);

            Assert.AreEqual(excpectedValue, _sessionManager.Get<string>(key));
        }

        [Test]
        public void can_get_non_existing_session_variable()
        {
            string key = "test_key";

            bool result = _sessionManager.TryGet<bool>(key);

            Assert.AreEqual(false, result);
        }
    }
}
